<template>
  <div class="login_container">
      <div class="login_box">
        <div class="avatar_box">
          <img src="../assets/logo.png" >
        </div>
        <el-form :model="loginForm" ref="loginForm" :rules="loginFormRules" class="login_form" label-width="0px">
          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="el-icon-edit"></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns">
            <el-button type="primary" @click=login>登录</el-button>
            <el-button type="info" @click=resetLoginForm>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10之间', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    // 表单重置
      resetLoginForm(){
        // console.log(this)
        this.$refs.loginForm.resetFields();
      },
    // 表单验证
      login(){
        this.$refs.loginForm.validate(async valid=>{
          if(!valid) return;
          // 返回结果是promies，所以通过async 和 await解析
          // 通过{属性名：重命名}的写法对具体属性解构
          const {data:result} = await this.$http.post('login', this.loginForm);
          if(result.meta.status!=200) return this.$message.error("登陆失败")
          this.$message.success("登陆成功")
          //获取token，保存seesionStorage中，跳转页面
          window.sessionStorage.setItem("token",result.data.token);

          this.$router.push('/home');
          
        })

      }
  }
}
</script>

<style lang="less" scoped>
  .login_container{
    height: 100%;
    background-color:#2b4d6b;
    display: flex;
    display: -webkit-flex;
    align-items:center;
    justify-content:center;
  }
  .login_box{
    position: relative;
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
    .login_form{
      position: absolute;
      left: 0;
      bottom: 0;
      right: 0;
      padding: 20px;
      box-sizing: border-box;
      .btns{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
  .avatar_box{
    height: 130px;
    width: 130px;
    padding: 10px;
    border:1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    margin: 0 auto;
    transform: translateY(-50%);
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: #eee;
    }
  }
</style>